#!/bin/bash

# postinst script for system-trusted-global-zone
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
        chown root:bin $BASEDIR/usr/share/man/man8
	chown root:bin $BASEDIR/lib
	chown root:bin $BASEDIR/lib/svc
	chown root:bin $BASEDIR/lib/svc/method
	chown root:sys $BASEDIR/etc
	chown root:sys $BASEDIR/etc/security
	chown root:sys $BASEDIR/etc/security/audio
	chown root:sys $BASEDIR/etc/security/lib
	chown root:sys $BASEDIR/etc/security/tsol
	chown root:sys $BASEDIR/etc/zones
	chown root:sys $BASEDIR/lib/svc/manifest
	chown root:sys $BASEDIR/lib/svc/manifest/network
	chown root:sys $BASEDIR/lib/svc/manifest/system
	chown root:sys $BASEDIR/var
	chown root:sys $BASEDIR/var/tsol
	chown root:sys $BASEDIR/var/tsol/doors
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/zones/SUNWtsoldef.xml ] && rm -f $BASEDIR/etc/zones/SUNWtsoldef.xml
	else
	    chmod 0444 "$BASEDIR/etc/zones/SUNWtsoldef.xml"
	    chown root:bin "$BASEDIR/etc/zones/SUNWtsoldef.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/method/svc-labeld ] && rm -f $BASEDIR/lib/svc/method/svc-labeld
	else
	    chmod 0555 "$BASEDIR/lib/svc/method/svc-labeld"
	    chown root:bin "$BASEDIR/lib/svc/method/svc-labeld"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/method/svc-tnctl ] && rm -f $BASEDIR/lib/svc/method/svc-tnctl
	else
	    chmod 0555 "$BASEDIR/lib/svc/method/svc-tnctl"
	    chown root:bin "$BASEDIR/lib/svc/method/svc-tnctl"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/method/svc-tnd ] && rm -f $BASEDIR/lib/svc/method/svc-tnd
	else
	    chmod 0555 "$BASEDIR/lib/svc/method/svc-tnd"
	    chown root:bin "$BASEDIR/lib/svc/method/svc-tnd"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/label_encodings ] && rm -f $BASEDIR/etc/security/tsol/label_encodings
	else
	    [ -f $BASEDIR/etc/security/tsol/label_encodings ] || cp -ax $BASEDIR/usr/share/doc/system-trusted-global-zone/examples/label_encodings $BASEDIR/etc/security/tsol/label_encodings
	    chmod 0400 "$BASEDIR/etc/security/tsol/label_encodings"
	    chown root:sys "$BASEDIR/etc/security/tsol/label_encodings"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/label_encodings.example ] && rm -f $BASEDIR/etc/security/tsol/label_encodings.example
	else
	    chmod 0444 "$BASEDIR/etc/security/tsol/label_encodings.example"
	    chown root:sys "$BASEDIR/etc/security/tsol/label_encodings.example"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/label_encodings.gfi.multi ] && rm -f $BASEDIR/etc/security/tsol/label_encodings.gfi.multi
	else
	    chmod 0444 "$BASEDIR/etc/security/tsol/label_encodings.gfi.multi"
	    chown root:sys "$BASEDIR/etc/security/tsol/label_encodings.gfi.multi"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/label_encodings.gfi.single ] && rm -f $BASEDIR/etc/security/tsol/label_encodings.gfi.single
	else
	    chmod 0444 "$BASEDIR/etc/security/tsol/label_encodings.gfi.single"
	    chown root:sys "$BASEDIR/etc/security/tsol/label_encodings.gfi.single"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/label_encodings.multi ] && rm -f $BASEDIR/etc/security/tsol/label_encodings.multi
	else
	    chmod 0444 "$BASEDIR/etc/security/tsol/label_encodings.multi"
	    chown root:sys "$BASEDIR/etc/security/tsol/label_encodings.multi"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/label_encodings.single ] && rm -f $BASEDIR/etc/security/tsol/label_encodings.single
	else
	    chmod 0444 "$BASEDIR/etc/security/tsol/label_encodings.single"
	    chown root:sys "$BASEDIR/etc/security/tsol/label_encodings.single"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/manifest/network/tnctl.xml ] && rm -f $BASEDIR/lib/svc/manifest/network/tnctl.xml
	else
	    chmod 0444 "$BASEDIR/lib/svc/manifest/network/tnctl.xml"
	    chown root:sys "$BASEDIR/lib/svc/manifest/network/tnctl.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/manifest/network/tnd.xml ] && rm -f $BASEDIR/lib/svc/manifest/network/tnd.xml
	else
	    chmod 0444 "$BASEDIR/lib/svc/manifest/network/tnd.xml"
	    chown root:sys "$BASEDIR/lib/svc/manifest/network/tnd.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/lib/svc/manifest/system/labeld.xml ] && rm -f $BASEDIR/lib/svc/manifest/system/labeld.xml
	else
	    chmod 0444 "$BASEDIR/lib/svc/manifest/system/labeld.xml"
	    chown root:sys "$BASEDIR/lib/svc/manifest/system/labeld.xml"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/lib/disk_clean ] && rm -f $BASEDIR/etc/security/lib/disk_clean
	else
	    chmod 0555 "$BASEDIR/etc/security/lib/disk_clean"
	    chown root:sys "$BASEDIR/etc/security/lib/disk_clean"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/lib/wdwmsg ] && rm -f $BASEDIR/etc/security/lib/wdwmsg
	else
	    chmod 0555 "$BASEDIR/etc/security/lib/wdwmsg"
	    chown root:sys "$BASEDIR/etc/security/lib/wdwmsg"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/lib/wdwwrapper ] && rm -f $BASEDIR/etc/security/lib/wdwwrapper
	else
	    chmod 0555 "$BASEDIR/etc/security/lib/wdwwrapper"
	    chown root:sys "$BASEDIR/etc/security/lib/wdwwrapper"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/devalloc_defaults ] && rm -f $BASEDIR/etc/security/tsol/devalloc_defaults
	else
	    [ -f $BASEDIR/etc/security/tsol/devalloc_defaults ] || cp -ax $BASEDIR/usr/share/doc/system-trusted-global-zone/examples/devalloc_defaults $BASEDIR/etc/security/tsol/devalloc_defaults
	    chmod 0644 "$BASEDIR/etc/security/tsol/devalloc_defaults"
	    chown root:sys "$BASEDIR/etc/security/tsol/devalloc_defaults"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/tnrhdb ] && rm -f $BASEDIR/etc/security/tsol/tnrhdb
	else
	    [ -f $BASEDIR/etc/security/tsol/tnrhdb ] || cp -ax $BASEDIR/usr/share/doc/system-trusted-global-zone/examples/tnrhdb $BASEDIR/etc/security/tsol/tnrhdb
	    chmod 0644 "$BASEDIR/etc/security/tsol/tnrhdb"
	    chown root:sys "$BASEDIR/etc/security/tsol/tnrhdb"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/tnrhtp ] && rm -f $BASEDIR/etc/security/tsol/tnrhtp
	else
	    [ -f $BASEDIR/etc/security/tsol/tnrhtp ] || cp -ax $BASEDIR/usr/share/doc/system-trusted-global-zone/examples/tnrhtp $BASEDIR/etc/security/tsol/tnrhtp
	    chmod 0644 "$BASEDIR/etc/security/tsol/tnrhtp"
	    chown root:sys "$BASEDIR/etc/security/tsol/tnrhtp"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/tnzonecfg ] && rm -f $BASEDIR/etc/security/tsol/tnzonecfg
	else
	    [ -f $BASEDIR/etc/security/tsol/tnzonecfg ] || cp -ax $BASEDIR/usr/share/doc/system-trusted-global-zone/examples/tnzonecfg $BASEDIR/etc/security/tsol/tnzonecfg
	    chmod 0644 "$BASEDIR/etc/security/tsol/tnzonecfg"
	    chown root:sys "$BASEDIR/etc/security/tsol/tnzonecfg"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/etc/security/tsol/relabel ] && rm -f $BASEDIR/etc/security/tsol/relabel
	else
	    [ -f $BASEDIR/etc/security/tsol/relabel ] || cp -ax $BASEDIR/usr/share/doc/system-trusted-global-zone/examples/relabel $BASEDIR/etc/security/tsol/relabel
	    chmod 0755 "$BASEDIR/etc/security/tsol/relabel"
	    chown root:sys "$BASEDIR/etc/security/tsol/relabel"
	fi
	# [ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/etc/security/lib && ln -f -s ./audio_clean $BASEDIR/etc/security/lib/audio_clean.windowing)
	# [ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/etc/security/lib && ln -f -s ./disk_clean $BASEDIR/etc/security/lib/disk_clean.windowing)
	# [ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/etc/security/lib && ln -f -s ./st_clean $BASEDIR/etc/security/lib/st_clean.windowing)
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

